<template>
  <div>
    <el-card>
      <el-table :data="packageList" style="width: 100%">
        <el-table-column type="expand">
          <template slot-scope="props">
            <el-form label-position="left" inline class="demo-table-expand">
              <el-form-item label="奖励名称">
                <span>{{ props.row.shopName }}</span>
              </el-form-item>
              <el-form-item label="奖励详情">
                <span>{{ props.row.shopIntroduce }}</span>
              </el-form-item>
              <el-form-item label="数量">
                <span>{{ props.row.amount }}</span>
              </el-form-item>
              <el-form-item label="兑现限时">
                <span>{{ props.row.timeout }} (小时)</span>
              </el-form-item>
            </el-form>
          </template>
        </el-table-column>
        <el-table-column label="奖励名称" prop="shopName"></el-table-column>
        <el-table-column label="奖励详情" prop="shopIntroduce"></el-table-column>
        <el-table-column label="数量" prop="amount"></el-table-column>
        <el-table-column label="兑现限时" prop="timeout"></el-table-column>
        <el-table-column label="使用">
          <template slot-scope="pro">
            <el-button type="primary" @click="conversionDialog(pro.row.id)">使用</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->

      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pageNum"
        :page-sizes="[2, 5, 10]"
        :page-size="queryInfo.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>

    <el-dialog
      title="使用数量"
      :visible.sync="conversionVisible"
      width="40%"
      @close="buyDialogClosed"
      :close-on-click-modal="false"
      destroy-on-close="true"
    >
      <template>
        <el-form ref="buyTaskFormRef" :model="conversion" label-width="80px">
          <el-form-item prop="conversion">
            <el-slider v-model="conversion.amount" show-input></el-slider>
          </el-form-item>
          <el-form-item class="buyButton">
            <el-button @click="conversionVisible = false; this.conversion = {}">取 消</el-button>
            <el-button type="primary" @click="use">确 定</el-button>
          </el-form-item>
        </el-form>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { secoondaryGetPackageList } from "@/utils/SecondaryApi";
import { secondaryUsePackge } from "@/utils/SecondaryApi";
export default {
  data() {
    return {
      conversion: {
        id: 0,
        amount: 0,
      },
      conversionVisible: false,
      queryInfo: {
        query: "",
        // 当前页数
        pageNum: 1,
        // 每页显示多少数据
        pageSize: 10,
        isType: 2,
      },
      packageList: [],
      total: 0,
    };
  },
  created() {
    this.getPackageList();
  },
  methods: {
    async use() {
      const { data: res } = await secondaryUsePackge(this.conversion);
      if (res.meta.status !== 200) {
        this.conversionVisible = false;

        return this.$message.error("使用奖励失败");
      }
      this.$message.success("兑换成功！");
      this.conversion = {};
      this.getPackageList();
      this.conversionVisible = false;
    },
    conversionDialog(id) {
      this.conversionVisible = true;
      this.conversion.id = id;
    },
    buyDialogClosed() {
      this.$refs.buyTaskFormRef.resetFields();
    },
    async getPackageList() {
      const { data: res } = await secoondaryGetPackageList(this.queryInfo);
      if (res.meta.status !== 200) {
        return this.$message.error("获取背包列表失败");
      }
      this.packageList = res.data.list;
      this.total = res.data.total;
    },
    isReceive(flag) {
      return flag == 1;
    },
    handleSizeChange(newSize) {
      console.log(newSize);
      this.queryInfo.pageSize = newSize;
      this.getPackageList();
    },
    // 监听 页码值 改变事件
    handleCurrentChange(newSize) {
      // console.log(newSize)
      this.queryInfo.pageNum = newSize;
      this.getPackageList();
    },
    finishTaskDisabled(flag) {
      return flag === 1;
    },
  },
};
</script>
<style lang="less" scoped>
.buyButton {
  margin-left: 360px;
}
.demo-table-expand {
  font-size: 0;
}
.demo-table-expand label {
  width: 90px;
  color: #99a9bf;
}
.demo-table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
  width: 50%;
}
</style>